package com.luxk.db.data;

import java.io.IOException;

public abstract class ByteObject {
	
	protected byte[] rawBytes;
	protected int offset;
	protected int size;
	
	public void init(byte[] b, int offset) throws IOException {
		this.rawBytes = b;
		this.offset = offset;
		this.size = b.length;
		
		deserialize();
	}
	
	public void init(byte[] b, int offset, int size) throws IOException {
		this.rawBytes = b;
		this.offset = offset;
		this.size = size;
		
		deserialize();
	}
	
	public byte[] getRawBytes() {
		return this.rawBytes;
	}
	
	public int getStartOffset() {
		return this.offset;
	}
	
	public int writeTo(byte[] target, int offset) {
		if(offset < 0 || offset >= target.length) return 0;
		System.arraycopy(this.rawBytes, 0, target, offset,
				this.rawBytes.length);
		return this.rawBytes.length;
	}
	
	public byte[] duplicate() {
		byte[] res = new byte[this.size];
		System.arraycopy(this.rawBytes, offset, res, 0, res.length);
		return res;
	}
	
	protected abstract void deserialize() throws IOException;
	public abstract String dump();

}
